﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mediatheque.Areas.Admin.Models;

namespace mediatheque.Areas.Admin.Controllers
{
    [Authorize]
    public class AlbumsController : Controller
    {
        private AlbumsModel cd = new AlbumsModel();
        //
        // GET: /Admin/Albums/

        public ActionResult Index()
        {
            var list = cd.getAlbumsList();

            if (list == null)
                return View("NotFound");
            else
                return View(list);
        }

        //
        // GET: /Admin/Albums/Details/5

        public ActionResult Details(int id)
        {
            //liste des albums (parent_id = 0)
            var chapitres = cd.getChapitresByOeuvreId(id);

            if (chapitres == null)
                return View("NotFound");
            else {
                //liste des titres par album
                List<int> parentIds = new List<int>();
                foreach (var item in chapitres)
                {
                    parentIds.Add(item.parent_id);
                }
                ViewData["albums"] = cd.getByList(parentIds);
                ViewData["id"] = id;
                return View(chapitres);
            }

            
        }

        //
        // GET: /Admin/Albums/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Admin/Albums/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                chapitre c = new chapitre
                {
                    oeuvre_id = int.Parse(Request.Form["oeuvre_id"]),
                    parent_id = int.Parse(Request.Form["parent_id"]),
                    titre = Request.Form["titre"],
                    duree = int.Parse(Request.Form["duree"]),
                    nb_pages = int.Parse(Request.Form["nb_pages"])
                };
                cd.chapitres.AddObject(c);
                cd.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        
        
        //
        // GET: /Admin/Albums/Edit/5
 
        public ActionResult Edit(int id)
        {
            var ca = cd.chapitres.First(c => c.id == id);
            return View(ca);
        }

        //
        // POST: /Admin/Albums/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                chapitre ca = cd.chapitres.First(c => c.id == id);

                ca.oeuvre_id = int.Parse(Request.Form["oeuvre_id"]);
                ca.parent_id = int.Parse(Request.Form["parent_id"]);
                ca.titre = Request.Form["titre"];
                ca.duree = int.Parse(Request.Form["duree"]);
                ca.nb_pages = int.Parse(Request.Form["nb_pages"]);

                cd.SaveChanges();
                return RedirectToAction("Index");

            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Musique/Delete/5
 
        public ActionResult Delete(int id)
        {
            chapitre ca = cd.chapitres.FirstOrDefault(c => c.id == id);
            if(ca==null)
                return View("NotFound");
            else
                return View(ca);
        }

        //
        // POST: /Admin/Musique/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                chapitre ca = cd.chapitres.FirstOrDefault(c => c.id == id);
                cd.chapitres.DeleteObject(ca);
                cd.SaveChanges(); 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Albums/Addtrack?OeuvreID=5&&albumId=10

        public ActionResult Addtrack(int oeuvreId, int albumId)
        {
            chapitre c = new chapitre { oeuvre_id = oeuvreId, parent_id = albumId };
            ViewData["users"] = from u in cd.users
                                select new { u.id, u.username };
            return View(c);
        }

        //
        // POST: /Admin/Albums/Addtrack?OeuvreID=5&&albumId=10

        [HttpPost]
        public ActionResult Addtrack(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                chapitre c = new chapitre
                {
                    oeuvre_id = int.Parse(Request.Form["oeuvre_id"]),
                    parent_id = int.Parse(Request.Form["parent_id"]),
                    titre = Request.Form["titre"],
                    duree = int.Parse(Request.Form["duree"]),
                    nb_pages = int.Parse(Request.Form["nb_pages"])
                };
                cd.chapitres.AddObject(c);
                cd.SaveChanges();
                return RedirectToAction("Details", new { id = c.oeuvre_id });
            }
            catch
            {
                return RedirectToAction("Inddex");
            }
        }

        //
        // GET: /Admin/Albums/EditTrack/5

        public ActionResult EditTrack(int id)
        {
            var ca = cd.chapitres.First(c => c.id == id);
            return View(ca);
        }

        //
        // POST: /Admin/Albums/EditTrack/5

        [HttpPost]
        public ActionResult EditTrack(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                chapitre ca = cd.chapitres.First(c => c.id == id);
                
                    ca.oeuvre_id = int.Parse(Request.Form["oeuvre_id"]);
                    ca.parent_id = int.Parse(Request.Form["parent_id"]);
                    ca.titre = Request.Form["titre"];
                    ca.duree = int.Parse(Request.Form["duree"]);
                    ca.nb_pages = int.Parse(Request.Form["nb_pages"]);
                
                cd.SaveChanges();
                return RedirectToAction("Details", new { id = ca.oeuvre_id });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Musique/DeleteTrack/5

        public ActionResult DeleteTrack(int id)
        {
            chapitre ca = cd.chapitres.First(c => c.id == id);
            if (ca == null)
                return View("not found");
            else
                return View(ca);
        }

        //
        // POST: /Admin/Musique/DeleteTrack/5

        [HttpPost]
        public ActionResult DeleteTrack(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                chapitre ca = cd.chapitres.First(c => c.id == id);
                var oeuvre =  ca.oeuvre_id;
                cd.chapitres.DeleteObject(ca);
                cd.SaveChanges();
                return RedirectToAction("Details", new { id = oeuvre });
            }
            catch
            {
                return View();
            }
        }

    }
}
